<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
          <el-form-item label="课程表名称" prop="courseName">
            <el-input v-model="queryParams.courseName" placeholder="请输入课程表名称" clearable style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="courseList">
          <el-table-column label="课程表名称" align="center" prop="courseName" />
          <el-table-column label="创建时间" align="center" prop="createTime">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="980px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="课程表名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程表名称" maxlength="20" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="课程表">
          <el-table :data="form.courseTime" border max-height="500">
            <el-table-column label="时间" align="center" width="180">
              <template slot-scope="scope">
                <el-time-picker :picker-options="{ format: 'HH:mm' }" value-format="HH:mm" arrow-control is-range
                  :editable="false" v-model="scope.row.timeRange" style="width: 140px; !important"
                  placeholder="开始时间" @change="timeChange($event, scope)"></el-time-picker>
              </template>
            </el-table-column>
            <el-table-column :label="'星期' + item" class-name="week-style" align="center"
              v-for="(item, index) in week"></el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-delete"
                  @click="deleteCourseTime(scope)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-button type="primary" @click="addCourseTime" style="margin-top: 20px;">添加</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourse, getCourse, addCourse, updateCourse, deleteCourse, checkIsExist } from "@/api/spoken/course";
const week = ['一', '二', '三', '四', '五', '六', '日'];

export default {
  name: "Category",
  data() {
    return {
      week,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      courseList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: undefined,
      },
      form: {},
      // 表单校验
      rules: {
        courseName: [
          { required: true, message: "课程表名称不能为空", trigger: "blur" },
        ]
      }
    };
  },
  watch: {
  },
  created() {
    this.getList();
    this.form.courseTime = [
      {timeRange: '', startTime: '', endTime: ''}
    ]
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseName: undefined,
        courseId: undefined,
        courseTime: [
          {timeRange: '', startTime: '', endTime: ''}
        ]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.title = "添加分类";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // checkIsExist(row.courseId).then(res => {
        this.reset();
        getCourse(row.courseId).then(response => {
          response.data.courseTime.forEach(item => {
            item.timeRange = [item.startTime, item.endTime]
          })
          this.form = response.data;
          this.title = "修改课程表";
          this.open = true;
        });
      // })
      
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 校验课程表时间段
          let flag = true
          this.form.courseTime.forEach(item => {
            if(!item.timeRange){
              flag = false
            }
          })
          if(!this.form.courseTime.length){
            this.$modal.msgError('请添加时间')
            return 
          }
          if(!flag){
            this.$modal.msgError('请补全时间')
            return 
          }
          this.form.cour
          if (this.form.courseId != undefined) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除？').then(function () {
        return deleteCourse(row.courseId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 删除课程时间段按钮操作 */
    deleteCourseTime(scope) {
      this.form.courseTime.splice(scope.$index, 1)
    },
    addCourseTime(){
      this.form.courseTime.push({timeRange: '', startTime: '', endTime: ''})
    },
    timeChange(e, scope){
      console.log(e)
      let timeObj = this.form.courseTime[scope.$index]
      timeObj.startTime = e[0]
      timeObj.endTime = e[1]
    }
  }
};
</script>

<style>
.week-style {
  width: 70px;
  height: 70px;
}
</style>